#!/bin/env sh
cd "$(dirname "$0")"; set -o errexit

IMGNM="ctnetcd" BASE="imginit"

#拉取基础容器
echo "测试基础镜像.."
buildah images -n "$BASE" 2>/dev/null || \
echo "基础镜像[$BASE]需要构建,已终止.."

#创建容器,确定构建
echo "启动构建容器.."
CONTAINER="$( buildah from "$BASE" )"
CTROOTDIR="$( buildah mount "$CONTAINER" )"
#LCWORKDIR="$( mktemp -d -p ./ )"

#配置标签
buildah config --label maintainer="Zhixa<reamyx@126.com>" "$CONTAINER"

#复制目标文件
buildah copy "$CONTAINER" "etcd" "/srv/etcd/"

#配置工作目录
buildah config --workingdir "/srv/etcd/" "$CONTAINER"

#基础工具软件安装
echo "执行etcd软件安装.."
git clone https://gitee.com/reamyx/etcdbin
buildah copy "$CONTAINER" ./etcdbin/linux-x86/etcd ./etcdbin/linux-x86/etcdctl /usr/bin
buildah copy "$CONTAINER" ./etcdbin/linux-x86/etcdkeeper /srv/etcdkeeper
rm -rf ./etcdbin

#清理临时文件
echo "清理临时文件.."
buildah run "$CONTAINER" rm -rf /tmp/* /var/cache/* ~/*

#设置ENTRYPOINT

buildah config --env "IMG_NAME=$IMGNM" "$CONTAINER"
buildah config --env "SRVNAME=etcd" "$CONTAINER"
buildah config --env "SRVSTART=./etcdstart.sh" "$CONTAINER"
buildah config --entrypoint "initstart" "$CONTAINER"

#提交镜像
echo "生成目标镜像.."
buildah unmount "$CONTAINER"
buildah commit --squash --rm "$CONTAINER" "$IMGNM"

echo "构建完成: $IMGNM"


